package com.hsh.maoduo.util;

import cn.hutool.core.util.ReflectUtil;
import com.hsh.maoduo.pojo.export.CountExport;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class MyBeanUtils extends BeanUtils {
    public static <S, T> List<T> copyList(List<S> sourceList, Class<T> tClass) {
        List<T> targetList = new ArrayList<>(sourceList.size());
        for (S s : sourceList) {
            try {
                T t = tClass.getConstructor().newInstance();
                BeanUtils.copyProperties(s, t);
                targetList.add(t);
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
                     NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
        }
        return targetList;
    }

    public static <S, T> T copy(S s, Class<T> clazz) {
        T t;
        try {
            t = clazz.getConstructor().newInstance();
            BeanUtils.copyProperties(s, t);
        } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return t;
    }

    /**
     * 将 addT 加入到 oldList 中
     * 如果 addT 和 oldTList 中的某个元素 的simpleGoodsNam字段 值相同，则num相加
     * 反之，将addT加入到oldList中
     *
     * @param oldList
     * @param addT
     * @return
     */
    public static <T> List<T> addListBySimpleName(List<T> oldList, T addT) {
        try {
            for (T oldT : oldList) {
                Field goodsNameFile = addT.getClass().getDeclaredField("goodsName");
                Field shopIdFile = addT.getClass().getDeclaredField("shopId");
                Field numFile = addT.getClass().getDeclaredField("num");
                shopIdFile.setAccessible(true);
                goodsNameFile.setAccessible(true);
                numFile.setAccessible(true);
                ReflectUtil.getFields(addT.getClass());

                Object addShopId = shopIdFile.get(addT);
                Object oldShopId = shopIdFile.get(oldT);
                Object addGoodsName = goodsNameFile.get(addT);
                Object oldGoodsName = goodsNameFile.get(oldT);
                Integer addNum = (Integer) numFile.get(addT);
                Integer oldNum = (Integer) numFile.get(oldT);

                if (addGoodsName != null && Objects.equals(addGoodsName, oldGoodsName)
                        && addShopId != null && Objects.equals(addShopId, oldShopId)) {
                    numFile.set(oldT, oldNum + addNum);
                    return oldList;
                }
            }
        } catch (IllegalAccessException | NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
        oldList.add(addT);
        return oldList;
    }

    /**
     * 将Object对象里面的属性和值转化成Map对象
     *
     * @param obj
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String, Object> objectToMap(Object obj) {
        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = obj.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            field.setAccessible(true);
            String fieldName = field.getName();
            Object value = null;
            try {
                value = field.get(obj);
            } catch (IllegalAccessException e) {

            }
            map.put(fieldName, value);
        }
        return map;
    }

    public static void main(String[] args) {
        Field[] fields = CountExport.class.getDeclaredFields();
        Arrays.stream(fields).forEach(v -> v.setAccessible(true));

    }

}
